home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / etc / resolvconf / update.d / dnscache < prev    next >
Encoding:
Text File  |  2008-12-06  |  3.1 KB  |  121 lines

  1. #!/bin/sh
  2. #
  3. # Script to update every forwarding djbdns dnscache instance running
  4. # on the local machine
  5. #
  6. # this script sets up every FORWARDONLY dnscache managed by either
  7. # daemontools or runit on this machine to use the dynamically-offered
  8. # nameservers for the default '@'
  9. # Assumption: On entry, PWD contains the resolv.conf-type files
  10. #
  11. # If no nameservers are offered, we ask the dnscache instances to fall
  12. # back to the root nameservers listed in /etc/dnsroots.global
  13. #
  14. # Licensed under the GNU GPL.  See /usr/share/doc/resolvconf/copyright.
  15. #
  16. # Written by Daniel Kahn Gillmor <dkg-debian.org@fifthhorseman.net>
  17. # based on similar scripts by Thomas Hood
  18.  
  19. set -e
  20. PATH=/usr/sbin:/usr/bin:/sbin:/bin
  21.  
  22. [ -x /usr/bin/dnscache ] || exit 0
  23. [ -x /lib/resolvconf/list-records ] || exit 1
  24.  
  25. ETC=/etc
  26. ETCRESOLVCONF="${ETC}/resolvconf"
  27. TMPFILE="${ETCRESOLVCONF}/run/dnscache_new.$$"
  28. DEFAULTROOTS="${ETC}/dnsroots.global"
  29.  
  30. CACHES=""
  31.  
  32. # Which directories do we scan?
  33. # /var/lib/svscan: daemontools, built "the debian way" (LFS-compliant)
  34. # /service: daemontools, built "the djb way"
  35. # /var/service: runit
  36.  
  37. for SERVICEDIR in /var/lib/svscan /service /etc/service ; do
  38.     # We'll only manage caches actually managed by daemontools or runit.
  39.     if [ -d "$SERVICEDIR" ] ; then
  40.         OLDCWD=`pwd`
  41.         cd "$SERVICEDIR"
  42.         for SVC in * ; do
  43.             SVC="$SERVICEDIR/$SVC"
  44.             if \
  45.                 [ -d "$SVC" ] \
  46.                 && [ -f "$SVC/run" ] \
  47.                 && [ ! -f "$SVC/down" ] \
  48.                 && [ -d "$SVC/root" ] \
  49.                 && [ -d "$SVC/root/servers" ] \
  50.                 && [ -d "$SVC/root/ip" ] \
  51.                 && [ -s "$SVC/env/FORWARDONLY" ] \
  52.                 && grep -q dnscache "$SVC/run"
  53.             then
  54.                 CACHES="$SVC $CACHES"
  55.             fi
  56.         done
  57.         cd "$OLDCWD"
  58.     fi
  59. done
  60.  
  61. # Stores arguments (minus duplicates) in RSLT, separated by spaces
  62. # Doesn't work properly if an argument itself contain whitespace
  63. uniquify()
  64. {
  65.     RSLT=""
  66.     while [ "$1" ] ; do
  67.         for E in $RSLT ; do
  68.             [ "$1" = "$E" ] && { shift ; continue 2 ; }
  69.         done
  70.         RSLT="${RSLT:+$RSLT }$1"
  71.         shift
  72.     done
  73. }
  74.  
  75. RSLVCNFFILES="$(/lib/resolvconf/list-records)"
  76.  
  77. ### Compile list of nameservers ###
  78. NMSRVRS=""
  79. if [ "$RSLVCNFFILES" ] ; then
  80.     uniquify $(sed -n 's/^[[:space:]]*nameserver[[:space:]]\+//p' $RSLVCNFFILES)
  81.     NMSRVRS="$RSLT"
  82. fi
  83.  
  84. clean_up() { rm -f "$TMPFILE" ; }
  85. trap clean_up EXIT
  86. clean_up
  87.  
  88. # If no nameservers were present, use the default root nameservers
  89. # provided by the djbdns package:
  90. if [ -z "$NMSRVRS" ] && [ -e "$DEFAULTROOTS" ] ; then
  91.     cp "$DEFAULTROOTS" "$TMPFILE"
  92. else
  93.     : > "$TMPFILE"
  94.     for N in $NMSRVRS ; do
  95.         echo "$N" >> "$TMPFILE"
  96.     done
  97. fi
  98.  
  99. # svc is the daemontools service controller
  100. SVC_CMD="$(which svc)" || :
  101. [ ! "$SVC_CMD" ] && [ -x /command/svc ] && SVC_CMD=/command/svc
  102.  
  103. # sv is the runit service controller
  104. SV_CMD="$(which sv)" || :
  105.  
  106. for CACHE in $CACHES ; do
  107.     cp "$TMPFILE" "$CACHE/root/servers/@"
  108.     # Restart the cache if it's already running:
  109.         if [ "$SV_CMD" ] && \
  110.         [ -e "$CACHE/supervise/pid" ] && \
  111.         [ -e "$CACHE/supervise/stat" ]; then
  112.             # services managed by runit have supervise/{pid,stat}
  113.             "$SV_CMD" restart "$CACHE"
  114.         else
  115.             # otherwise, it is probably managed by daemontools
  116.             [ "$SVC_CMD" ] && "$SVC_CMD" -t "$CACHE"
  117.         fi
  118. done
  119.  
  120.